#include "Engine.h"

namespace TSS
{
	Camera::Camera()
	{
		p_position = Vector3(0.0f,0.0f,10.0f);
		p_upDir = D3DXVECTOR3(0.0f,1.0f,0.0f);
		double ratio = 1024 / 768;
		setPerspective( TSS::PI / 4.0f, ratio, 1.0f, 10000.0f);

		this->Update();
	}

	Camera::~Camera()
	{

	}

	void Camera::setPerspective(double fov, double aspectRatio, double nearRange, double farRange)
	{
		p_fov = fov;
		p_aspectRatio = aspectRatio;
		p_nearRange = nearRange;
		p_farRange = farRange;

		//set the camera's perspective matrix
		D3DXMatrixPerspectiveFovLH(&this->p_matrixProj, (float)p_fov, 
			(float)p_aspectRatio, (float)p_nearRange, (float)p_farRange);
	}

	void Camera::setPosition(double x, double y, double z)
	{
		p_position.x = (float)x;
		p_position.y = (float)y;
		p_position.z = (float)z;
	}

	void Camera::setRotation(double x,double y,double z)
	{
		p_rotation.x = (float)x;
		p_rotation.y = (float)y;
		p_rotation.z = (float)z;

		//update rotation matrix
		D3DXMatrixRotationYawPitchRoll(&p_matrixRot, (float)p_rotation.x, (float)p_rotation.y, (float)p_rotation.z);
	}

	void Camera::Update()
	{
		//create the view matrix
		D3DXVECTOR3 pos = p_position.toD3DXVECTOR3();
		D3DXVECTOR3 target = p_target.toD3DXVECTOR3();
		D3DXMatrixLookAtLH(&p_matrixView, &pos, &target, &p_upDir);
	}

	void Camera::Rotate(double x, double y, double z)
	{
		p_rotation.x += (float)x;
		p_rotation.y += (float)y;
		p_rotation.z += (float)z;
	}

	void Camera::Look(double x, double y, double z)
	{
		p_target.x += (float)x;
		p_target.y += (float)y;
		p_target.z += (float)z;
	}

	void Camera::Move(double x, double y, double z)
	{
		p_position.x += (float)x;
		p_position.y += (float)y;
		p_position.z += (float)z;

		p_target.x += (float)x;
		p_target.y += (float)y;
		p_target.z += (float)z;
	}
};